//
//  BSSUploadImagApi.m
//  BrightSunnySky
//
//  Created by zhoujunchun on 2019/3/7.
//  Copyright © 2019 PURANG. All rights reserved.
//

#import "BSSUploadImagApi.h"
#import "UIImage+Resolution.h"
#import "YTKNetworkAgent.h"
#import "AFNetworking.h"

@interface BSSUploadImagApi ()

@property (nonatomic ,strong)UIImageView *backView;

@end
@implementation BSSUploadImagApi

#pragma mark - override Getters
-(instancetype)initWithURL:(NSString *)URLStr dic:(NSDictionary *)dic imageArray:(NSArray *)imageArray{
    self = [super initWithURL:URLStr dic:dic];
    if (self) {
        self.limit = NO;
        self.imageArray = [NSArray arrayWithArray:imageArray];
    }
    return self;
}
- (AFConstructingBlock)constructingBodyBlock {
    return ^(id<AFMultipartFormData> formData) {
        for (UIImage *image in self.imageArray) {
            NSData* data;
            NSString* mimeType;
            NSString* name;
            CGSize newSize = image.size;
            UIImage* newImage;
            
            if (newSize.width > 1000 || image.size.height > 1000){
                newImage = [image imageScaledToSize:CGSizeMake(newSize.width*0.5, newSize.height*0.5)];
            }else{
                newImage = image;
            }
//            if (UIImagePNGRepresentation(newImage)){
//返回为png图像。
//                data = UIImagePNGRepresentation(newImage);
//                name = [NSString stringWithFormat:@"Image%ld.png",(unsigned long)[self.imageArray indexOfObject:image]];
//                mimeType = @"image/png";
//            }else{
//返回为JPEG图像。
//                data = UIImageJPEGRepresentation(newImage, 1.0);
//                mimeType = @"image/jpg";
//                name = [NSString stringWithFormat:@"Image%ld.jpg",(unsigned long)[self.imageArray indexOfObject:image]];
//            }
            if (self.limit) {
                //返回为JPEG图像。
                data = UIImageJPEGRepresentation(newImage, 1.0);
                double bili = (double)2*1024*1024/data.length;
                if (data.length>1.9*1024*1024) {
                    data = UIImageJPEGRepresentation(newImage, bili);
                }
            }else{
                data = UIImageJPEGRepresentation(newImage, 0.7);
            }
            mimeType = @"image/jpg";
            name = [NSString stringWithFormat:@"Image%ld.jpg",(unsigned long)[self.imageArray indexOfObject:image]];
            
            if (_isFiles) {
                [formData appendPartWithFileData:data name:@"files" fileName:name mimeType:mimeType];
            }else{
                [formData appendPartWithFileData:data name:@"file" fileName:name mimeType:mimeType];
            }
        }
    };
}

- (void)startRequest:(BSSRequestBlock)requestBlock{
    NSLog(@"\n%@\n%@\n",
          [[YTKNetworkAgent sharedAgent] buildRequestUrl:self],
          self.requestArgument);
    [self constructingBodyBlock];
    [self startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        
        NSData *jsonData = [request.responseString dataUsingEncoding:NSUTF8StringEncoding];
        NSError *err;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                            options:NSJSONReadingMutableContainers
                                                              error:&err];
        
        [self handleSuccess:requestBlock
             responseObject:dic
            responseHeaders:request.responseHeaders];
        
    } failure:^(YTKBaseRequest *request) {
        
        [self handleFailure:requestBlock
                      error:request.error
            responseHeaders:request.responseHeaders];
    }];
}

- (NSDictionary *)requestHeaderFieldValueDictionary{
    NSMutableDictionary *dicts = [NSMutableDictionary dictionary];
    
    NSString *app_Version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    
    [dicts setValue:@"ios" forKey:@"mobileModel"];
    [dicts setValue:app_Version forKey:@"mobileVersion"];
    
    NSString *yyttoken = [[NSUserDefaults standardUserDefaults] stringForKey:@"yyttoken"];
    if (yyttoken.length) {
        [dicts setValue:yyttoken forKey:@"yyttoken"];
    }
    
    return [dicts copy];
}
@end
